<?php
/**
 * Created by PhpStorm.
 * User: yuliang
 * Date: 2017/6/5
 * Time: 下午9:12
 */

namespace app\admin\controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;

class Tool extends Controller
{
    // banner列表
    public function slideList(){
        $page = input('page')?input('page'):1;
        $pageSize = 10;
        $list = model('Slide')->field(['content','description','create_time'],true)->page($page,$pageSize)->order('id desc')->select();
        $count = model('Slide')->count();
        $result['page'] = ceil($count/$pageSize);
        $result['data'] = $list;
        return api_return('操作成功！',$result);
    }

    // banner详情
    public function slideDetail(){
        $id = input('id');
        $detail = model('Slide')->field(['create_time','update_time','content','description'],true)->where('id',$id)->find();
        if(empty($detail)){
            return api_error('40005','对不起，没有找到！');
        }else{
            return api_return('获取成功！',$detail);
        }
    }

    // banner添加
    public function addSlide(){

        $data = input('post.');
        $validate = $this->validate($data,'Slide');
        if(true !== $validate){
            return api_error('1000',$validate);
        }
        $file = request()->file('image');
        if($file){
            $file = $file->getInfo();
            $fileInfo = pathinfo($file['name']);
            if(isset($fileInfo['extension'])){
                $ext = $fileInfo['extension'];
            }else{
                $ext = 'jpg';
            }
            $upFile = $file['tmp_name'];
            $qiniu = config('qiniu');
            $auth = new Auth($qiniu['access_key'],$qiniu['secret_key']);
            $token = $auth->uploadToken($qiniu['bucket']);
            // 构建 UploadManager 对象
            $uploadMgr = new UploadManager();
            $fileName = 'upload/slide/'.date('Ymd').'/'.md5(input('post.name')).'.'.$ext;
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token,$fileName,$upFile);
            if($err !== null){
                return api_error('40001',$err['code'].$err['error']);
            }else{
                $data['image'] = $qiniu['site_url'].$ret['key'];
            }
        }else{
            return api_error('1000','请选择图片！');
        }

        $result = model('Slide')->isUpdate(false)->allowField(true)->save($data);
        if($result){
            return api_return('操作成功！',array());
        }else{
            return api_error('40002','添加banner失败！');
        }
    }

    // 修改banner
    public function editSlide(){
        $data = input('post.');
        $validate = $this->validate($data,'Slide');
        if(true !== $validate){
            return api_error('1000',$validate);
        }
        $file = request()->file('image');
        if($file){
            $file = $file->getInfo();
            $fileInfo = pathinfo($file['name']);
            if(isset($fileInfo['extension'])){
                $ext = $fileInfo['extension'];
            }else{
                $ext = 'jpg';
            }
            $upFile = $file['tmp_name'];
            $qiniu = config('qiniu');
            $auth = new Auth($qiniu['access_key'],$qiniu['secret_key']);
            $token = $auth->uploadToken($qiniu['bucket']);
            // 构建 UploadManager 对象
            $uploadMgr = new UploadManager();
            $fileName = 'upload/slide/'.date('Ymd').'/'.md5(input('post.name')).'.'.$ext;
            // 调用 UploadManager 的 putFile 方法进行文件的上传
            list($ret, $err) = $uploadMgr->putFile($token,$fileName,$upFile);
            if($err !== null){
                return api_error('40003',$err['code'].$err['error']);
            }else{
                $data['image'] = $qiniu['site_url'].$ret['key'];
            }
        }

        $result = model('Slide')->isUpdate(true)->allowField(true)->save($data,['id'=>$data['id']]);
        if($result !== false){
            return api_return('操作成功！',array());
        }else{
            return api_error('40004','修改banner失败！');
        }
    }

    // 操作状态
    public function operate(){
        $data['id'] = trim(input('post.id'),',');
        $data['status'] = input('post.status');
        $rule = [
            'id' => 'require',
            'status' => 'require|in:0,1,-1' // 0不显示，1显示
        ];
        $validate = $this->validate($data,$rule);
        if($validate !== true){
            return api_error('1000',$validate);
        }
        $where['id'] = ['in',$data['id']];
        $result = model('Slide')->isUpdate(true)->save(['status'=>$data['status']],$where);
        if($result !== false){
            return api_return('修改状态成功！',array());
        }else{
            return api_error('40006','修改失败！');
        }
    }
}